package com.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.remmend.req.UpdateRemmendReq;
import com.fire.bo.remmend.req.UpdateRemmendTitle;
import com.fire.common.RspObjBean;
import com.fire.service.RemmendService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/remmend")
@Api(value = "推荐产品", description = "推荐产品")
public class RemmendController
{
	@Autowired
	private RemmendService remmendService;
	
	@ApiOperation(value = "推荐列表", notes = "推荐列表", response = UpdateRemmendReq.class)
    @RequestMapping(value = "/findAllRemmend", method = RequestMethod.POST)
    public @ResponseBody RspObjBean findAllRemmend()
    {
       
        try
        {
        	List<UpdateRemmendReq> list = remmendService.findAllRemmend();
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, list,10);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
	@ApiOperation(value = "推荐标题", notes = "推荐标题", response = UpdateRemmendTitle.class)
    @RequestMapping(value = "/findRemmend", method = RequestMethod.POST)
    public @ResponseBody RspObjBean findRemmend()
    {
        try
        {
        	UpdateRemmendTitle info = remmendService.findRemmend();
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, info);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
	@ApiOperation(value = "修改标题", notes = "修改标题")
    @RequestMapping(value = "/updateRemmendTitle", method = RequestMethod.POST)
    public @ResponseBody RspObjBean updateRemmendTitle(@RequestBody @Valid UpdateRemmendTitle beans,BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
        	
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	remmendService.updateRemmendTitle(beans);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
	@ApiOperation(value = "修改列表", notes = "修改列表")
    @RequestMapping(value = "/updateRemend", method = RequestMethod.POST)
    public @ResponseBody RspObjBean updateRemend(@RequestBody @Valid UpdateRemmendReq beans,BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
        	
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	remmendService.updateRemend(beans);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
}
